package itsm.isperp.module.controller.app;

import itsm.isperp.framework.web.controller.BaseController;
import itsm.isperp.framework.web.formatter.DateEditor;
import itsm.isperp.framework.web.formatter.SqlTimeEditor;
import itsm.isperp.framework.web.formatter.TimestampEditor;
import itsm.isperp.framework.web.response.ResultMessage;
import itsm.isperp.module.dto.app.FullCalendarDTO;
import itsm.isperp.module.entity.app.AppCalendarConfig;
import itsm.isperp.module.service.app.AppCalendarConfigService;

import java.sql.Time;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 日历配置控制器
 * 
 * @author lizx
 * @date 2013-5-12
 * @version V1.0
 */
@Controller
@RequestMapping("app/calendar/config")
public class AppCalendarConfigController extends
		BaseController<AppCalendarConfig> {

	@InitBinder
	public void initBinder(WebDataBinder binder) {
		binder.registerCustomEditor(Date.class, new DateEditor());
		binder.registerCustomEditor(Timestamp.class, new TimestampEditor());
		binder.registerCustomEditor(Time.class, new SqlTimeEditor());

	}

	@Autowired
	protected AppCalendarConfigService appCalendarConfigService;

	@Override
	public AppCalendarConfigService getService() {
		return appCalendarConfigService;
	}

	@Override
	public String getPageTitle() {
		return "日历设置";
	}

	@RequestMapping("workdateEdit")
	public ModelAndView workdateEdit() {
		ModelAndView mv = new ModelAndView("app/calendar/config/workdateEdit");
		return mv;
	}

	@RequestMapping("listWorkdate")
	@ResponseBody
	public List<FullCalendarDTO> listWorkdate(String configId, Date start,
			Date end, HttpServletRequest req) throws Exception {

		List<FullCalendarDTO> list = this.getService().getWorkday(start, end,
				configId);

		return list;
	}

	@RequestMapping("setWokdate")
	@ResponseBody
	public ResultMessage setWokdate(String workdate, boolean isWorkday,
			String configId, HttpServletRequest req) throws Exception {

		this.getService().setWokdate(workdate, isWorkday, configId);

		return ResultMessage.success();
	}

}
